package org.wlgzs.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IShowStoreService;
import org.wlgzs.shop.service.IStoreService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@Api(tags = "这是商家管理者的接口", value = "BusinessController")
@RequestMapping("/business")
public class businessController {
    @Autowired
    IGoodsDateService iGoodsDateService;

    @Autowired
    IStoreService iStoreService;

    @Autowired
    IShowStoreService showStoreService;

    @ApiOperation(value = "店铺登陆跳转页面",httpMethod = "GET",response = String.class)
    @RequestMapping("/loginBusiness")
    public String loginBusiness(HttpSession session){
        UserDate date=(UserDate)session.getAttribute("user");
        if(date.getPower()!=2){
            return "merchant/businessBackgronud";
        }
        else {
            return "/";
        }
    }




    @ApiOperation(value = "跳转'business_index'页面",httpMethod = "POST")
    @RequestMapping("/backstageIndexJump")
    public ModelAndView business_indexJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/business_index");
        return modelAndView;
    }



    @ApiOperation(value = "跳转'tore_logon'页面",httpMethod ="GET" )
    @RequestMapping("/toreLogon")
    public ModelAndView toreLogon() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/store_logon");
        return modelAndView;
    }

    @ApiOperation(value = "跳转'business_personal'页面",httpMethod ="GET" )
    @RequestMapping("/businessPersonal")
    public ModelAndView business_persona() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("backstage_business/business_personal");
        return modelAndView;
    }

    @ApiOperation(value = "多文件上传",httpMethod ="GET" )
    @RequestMapping("/upFewFile")
    public Msg upFile(@RequestParam MultipartFile[] documentFile, HttpServletRequest request,HttpSession session){
        return Msg.fail();
    }

    @ApiOperation(value = "店铺显示",httpMethod ="GET" )
    @RequestMapping("/showStore")
    public ModelAndView showStoreByUser(int userId){
     ModelAndView modelAndView=new ModelAndView();
     modelAndView.addObject("store",iStoreService.selectStoreById(userId));
     return modelAndView;
    }

    @ApiOperation(value = "店铺显示",httpMethod ="GET" )
    @RequestMapping("/showStoreDate")
    public ModelAndView showAllGoods(int userId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("store",iStoreService.selectStoreById(userId));
        return modelAndView;
    }


    @ApiOperation(value = "跳转'businessStore'页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/businessStore")
    public ModelAndView backstageShopJump(@RequestParam(value = "page", defaultValue = "1") Integer page,HttpSession session) {
        UserDate userDate=(UserDate)session.getAttribute("user");
        ModelAndView modelAndView = new ModelAndView();
        PageHelper.startPage(page,5);
        List<Store> list=iStoreService.selectStoreById(userDate.getUserId());
        PageInfo pageInfo=new PageInfo(list,5);
        modelAndView.addObject("store",pageInfo);
        modelAndView.setViewName("backstage_business/business_store");
        return modelAndView;
    }

    @ApiOperation(value = "根据店铺跳转页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/goodsPageByStore")
    public ModelAndView backstageShopJump(int storeId) {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/backstage_business/business_index");
        List<GoodsData> list=iGoodsDateService.selectGoodsBystoreID(storeId);
        if(showStoreService.getStoreShow(storeId)!=null){
            modelAndView.addObject("storeShow",showStoreService.getStoreShow(storeId));
        }
        else{
            modelAndView.addObject("storeShow","|");
        }

        modelAndView.addObject("goodsDate",list);
        modelAndView.addObject("storeId",storeId);
        return modelAndView;
    }


    @ApiOperation(value = "根据店铺跳转页面",httpMethod = "POST", response =ModelAndView.class)
    @RequestMapping("/selectStoreLikeDateById")
    public ModelAndView seletStoreDate(@Param("date") String date,@RequestParam(value = "page", defaultValue = "1") Integer page,HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        if(date==null||date.equals("")){
            modelAndView.setViewName("redirect:business/businessStore");
            return modelAndView;
        }
        PageHelper.startPage(page,5);
        UserDate userDate=(UserDate)session.getAttribute("user");
        List<Store> list=iStoreService.selectStoreLikeDateByStireId(date,userDate.getUserId());
        System.out.println(list.size());
        PageInfo pageInfo=new PageInfo(list);
        modelAndView.addObject("selectStore",date);
        modelAndView.addObject("store",pageInfo);
        modelAndView.setViewName("/backstage_business/business_store");
        return modelAndView;
    }

    //店铺头部宣传滚动
    @ResponseBody
    @RequestMapping("/addHeadAll")
    public Msg addHeadPhoto(){
    //showStoreService.addHeadShow();
        return Msg.fail();
    }

    //添加推荐商品
    @ResponseBody
    @RequestMapping("/addShowGood")
    public Msg addShowGoods(GoodsData data){
        return showStoreService.addShowGood(data.getStoreId(),data.getPhotoUrl(),data.getGoodsName(),data.getId());
    }

    //单个增加头部宣传画
    @ResponseBody
    @RequestMapping("/addOneHeadPhoto")
    public Msg addOneHead(@RequestParam(value="files",required=false)MultipartFile file,HttpSession session, HttpServletRequest request) throws IOException {
     String name=showStoreService.upFile(file,session,request);
     String number=request.getParameter("number");
     String storeId=request.getParameter("storeId");
     return  showStoreService.addOneHeadShow(name,number,storeId);
}

    //删除商品
    @ResponseBody
    @RequestMapping("/delectGoods")
    public Msg delectGoods(int id){
        return showStoreService.delectGoods(id);
    }


    @RequestMapping("/jumpUpPage")
    public String jumpUpPage(){
        return"backstage_business/business_upload";
    }
}
